$(function () {
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;
    //----------------------------------------------------------------
    //  页面查询参数
    var query = {
        pagenum: 1, //页码值
        pagesize: 8, //每页显示几条数据
    }
    // 渲染分页功能
    function renderPage(total) {
        // console.log(total);
        laypage.render({
            elem: 'pageBox', //分页容器的id
            count: total, //数据总数，从服务端得到
            limit: query.pagesize, //每页显示几条数据
            curr: query.pagenum,
            layout: ["count", "limit", "prev", "page", "next", "skip"],
            limits: [8, 10, 15, 20],
            jump: function (obj, first) {
                query.pagenum = obj.curr;
                query.pagesize = obj.limit;
                // 首行不执行
                if (!first) {
                    initDeviceList();
                }
            },

        });
    }
    //------------------------------------------------------------------------------
    // 调用渲染列表信息的函数
    initDeviceList();
    //------------------------------------------------------------------------------
    // 获取列表
    function initDeviceList() {
        var username = window.parent.$('#name').val()
        var sno =  window.parent.$('#sno').val()
        query.username = username;
        query.sno = sno;
        $.ajax({
            method: 'POST',
            url: '/my/userinfo/users/',
            data: query,
            success: function (res) {
                var htmStr = template('tpl-table', res);
                $('tbody').html(htmStr)
                renderPage(res.total)
            }
        })
    }
  
    //----------------------------------------------------------------
    // 根据id查看设备图片
    $('tbody').on('click', '.btn_viewDevice', function () {
        // console.log('ok');
        var id = $(this).attr('data_id');
        // console.log(id);
        $.ajax({
            method: 'GET',
            url: '/my/deviceManage/viewDevice/' + id,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                // layer.msg('获取设备图片成功');
                // console.log(res);
                var htmStr = template('tpl_view_device', res.data);
                // console.log(htmStr);
                $('[name="view_device').html(htmStr);
                // $('tbody').html(htmStr)
                // form.val('form_view', res.data[0])
            }
        })
        // var device = $('body #img')[0].src;

        viewDevice_index = layer.open({
            type: 1,
            area: ['800px', '600px'],
            title: '查看设备图片',
            content: $('#tpl_view_device').html(),
            success: function () {
                form.render();
            }
        })

    })


    //-------------
//-=----------------------------------------------------------------
  //纪实编辑评分按钮绑点击事件
  var id =null;
  var edit__record_index_self = null;
  $('tbody').on('click', '.btn_record_edit', function () {
    edit__record_index_self = layer.open({
        type: 1,
        area: ['900px', ''],
        title: '伊犁银保监分局干部平时考核纪实表',
        content: $('#dialog_record_edit').html(),
        success: function () {
            form.render();
        }
    })
    id = $(this).attr('data_id')
    // console.log(id)
    $.ajax({
        method: 'GET',
        url: '/my/userinfo/user/' + id,
        success: function (res) {
            var data = res.data;
            // console.log(data);
            var table = $('table.layui-table');
            table.find('tbody tr').each(function (index, row) {
                $(row).find('td').each(function (cellIndex, cell) {
                    if ($(cell).find('input').length > 0) {
                        var inputName = $(cell).find('input').prop('name');
                        if (data[inputName]) {
                            $(cell).find('input').val(data[inputName]);
                        }
                    }
                });
            });
        }
    })
})
  
    //----------------------------------------------------------------
    //通过代理的形式，为 form_editlog 表单绑定 submit 事件
    $('body').on('submit', '#form_record_user_edit', function (e) {
        e.preventDefault();
        const serialize = $(this).serialize();
        const params = new URLSearchParams(serialize);
        params.append('id', id); 
        //const data = params.toString();
        const user_id = params.get('id');
        const date2 =  params.get('date2');
        const comp =  params.get('comp');
        const work_day =  params.get('work_day');
        const off_day =  params.get('off_day');
        const late_order =  params.get('late_order');
        const self_view =  params.get('self_view');
        const self_date = new Date().toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' });
        const data = {
            id:user_id,
            date2:date2,
            comp:comp,
            work_day:work_day,
            off_day:off_day,
            late_order:late_order,
            self_view:self_view,
            self_date:self_date
        }
        console.log(data)
        $.ajax({
           method: 'POST',
           url: '/my/userinfo/updateRecord/',
            data: data,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                initDeviceList();
                layer.msg('成功！')
                // 根据索引，关闭对应的弹出层
                layer.close(edit__record_index_self)
            }
        })
    });
    $('body').on('click', '#form_record_user_edit button[type="button"]', function (e) {
        e.preventDefault();
        layer.close(edit__record_index_self);
    });



//纪实查看评分按钮绑点击事件
  var id =null;
  var view_record_index = null;
$('tbody').on('click', '.btn_view_record', function () {
    view_record_index = layer.open({
        type: 1,
        area: ['900px', ''],
        title: '伊犁银保监分局干部平时考核纪实表',
        content: $('#dialog_record_view').html(),
        success: function () {
            form.render();
        }
    })
    id = $(this).attr('data_id')
    $.ajax({
        method: 'GET',
        url: '/my/userinfo/user/' + id,
        success: function (res) {
            var data = res.data;
            // console.log(data);
            var table = $('table.layui-table');
            table.find('tbody tr').each(function (index, row) {
                $(row).find('td').each(function (cellIndex, cell) {
                    if ($(cell).find('input').length > 0) {
                        var inputName = $(cell).find('input').prop('name');
                        if (data[inputName]) {
                            $(cell).find('input').val(data[inputName]);
                        }
                    }
                });
            });
        }
    })
});
$('body').on('click', '#form_record_view_User button[type="button"]', function (e) {
    e.preventDefault();
    layer.close(view_record_index);
});
})